﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

/*Eredeti
  * kep = new MiniKep();
 kep.Image = new BitmapImage(new Uri(img));
 kep.ImageText = System.IO.Path.GetFileName(img);
 ImageView.Children.Add(kep);*/

namespace ImageView
{
    /// <summary>
    /// Interaction logic for ViewThumbnails.xaml
    /// </summary>
    public partial class ViewThumbnails : UserControl, IViev
    {
        private Thread T;
        private string[] _imgs;
        private int _index;

        public ViewThumbnails()
        {
            InitializeComponent();
            _index = -1;
        }

        public int SelectedIndex
        {
            get { return _index; }
        }

        public void ClearItems()
        {
            ImageView.Children.Clear();
        }

        private void DoEvents()
        {
            DispatcherFrame f = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate(object arg)
            {
                DispatcherFrame fr = arg as DispatcherFrame;
                fr.Continue = false;
            }, f);
            Dispatcher.PushFrame(f);
        }

        private void ThreadFnc()
        {
            int i = 0;

            this.Dispatcher.Invoke((Action)delegate
            {
                App.Taskbar.SetTaskbarProgessState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.Indeterminate);
                LoadProgress.IsIndeterminate = true;
            });
            foreach (var img in _imgs)
            {
                this.Dispatcher.Invoke((Action)delegate
                {
                    MiniKep m = new MiniKep();
                    m.Image = App.Shell.GetThumbnail(img);
                    m.SetRotation(img);
                    m.VideoIconVisible = App.MainWin.IsVideo(img);
                    m.Margin = new Thickness(10);
                    m.ImageText = System.IO.Path.GetFileName(img);
                    m.KepMouseDown += new MouseButtonEventHandler(m_handler);
                    ImageView.Children.Add(m);
                    ++i;
                    if (i == 5)
                    {
                        DoEvents();
                        i = 0;
                    }
                });
            }
            this.Dispatcher.Invoke((Action)delegate
            {
                App.Taskbar.SetTaskbarProgessState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.NoProgress);
                LoadProgress.IsIndeterminate = false;
            });
        }

        private void m_handler(object sender, MouseButtonEventArgs e)
        {
            MiniKep s = (MiniKep)sender;
            int index = -1;
            for (int i = 0; i < ImageView.Children.Count; ++i)
            {
                if (ImageView.Children[i] == (UIElement)sender)
                {
                    index = i;
                    break;
                }
            }
            if (s.VideoIconVisible) App.MainWin.SwitchtoVideoView(index);
            else App.MainWin.SwitchtoImageView(index);
        }

        public void Fill(string[] imgsource)
        {
            _imgs = imgsource;
            T = new System.Threading.Thread(ThreadFnc);
            T.SetApartmentState(ApartmentState.STA);
            T.Start();
            ImageCount.Text = imgsource.Length.ToString();
        }

        public void TerminateThread()
        {
            if (T != null)
            {
                if (T.IsAlive) T.Abort();
            }
        }
    }
}
